package com.power.sifa.approve.executor.impl;

import com.plf.common.domain.ResponseDTO;
import com.plf.core.base.service.Ha_attachmentService;
import com.power.flowable.api.IPersonalFeign;
import com.power.sifa.util.executor.AbstractPostExecutor;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.St_admreview;
import com.power.sifa.approve.service.Apl_admre_auditService;
import com.power.sifa.approve.service.Apl_admreviewService;
import com.power.sifa.approve.service.Apl_mainService;
import com.power.sifa.approve.service.Aprv_process_materialsService;
import com.power.sifa.net.service.ApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * 受理所有拒绝操作
 */
@Component
@Slf4j
public class AdmreAuditRefuse extends AbstractPostExecutor {
    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private Apl_admre_auditService apl_admre_auditService;
    @Resource
    private Apl_admreviewService apl_admreviewService;
    @Resource
    private ApplyService applyService;
    @Resource
    private Ha_attachmentService ha_attachmentService;
    @Resource
    private Aprv_process_materialsService aprv_process_materialsService;
    @Resource
    private IPersonalFeign iPersonalFeign;

    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> execute() throws Exception {
        Apl_admre_audit auditObj = this.apl_admre_auditService.getById(super.applyid);
        Apl_main faMainObj = this.apl_mainService.getById(auditObj.getApplyid());
        // 1. 修改业务主表状态为 待受理
        this.apl_admreviewService.updateStatus(auditObj.getApplyid(), St_admreview.UNDO);

        // 2. 终止子流程
        ResponseDTO res = this.iPersonalFeign.terminationTask(this.userid, "调解失败", true, "", super.processinstanceid, this.remark, new ArrayList<>(0));
        if (0 != res.getCode()) {
            log.error("AdmreAuditRefuse 终止流程失败 -> msg[{}]", res.getMsg());
            new RuntimeException("终止流程失败" + res.getMsg());
        }
        // 2.1 激活父流程
        ResponseDTO activeRes = this.iPersonalFeign.activationProcess(faMainObj.getInstid());
        if (0 != activeRes.getCode()) {
            log.error("AdmreAuditAccept 激活父流程失败 -> msg[{}]", activeRes.getMsg());
            throw new RuntimeException("AdmreAuditAccept 激活父流程失败 -> " + activeRes.getMsg());
        }

//        // 3. 生成审批表 (注释暂不生成审批表)
//        Apl_main mainObj = apl_mainService.getById(super.applyid);
//        Ha_attachment attachment;
//        Map<String, Object> formData = this.applyService.formLoadField(mainObj.getFormid(), super.applyid);
//        // 3.1 审批表意见和日期字段
//        super.ApprovalField(formData);
//        try {
//            WordUtil wordUtil = new WordUtil("AdmreAuditPrincipal.docx");
//            wordUtil.replaceTable(formData);
//            OutputStream os = wordUtil.createWord();
//            InputStream is = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
//            byte[] pdfByte = PdfConvertUtil.word2pdfWithFont(is);
//            attachment = ha_attachmentService.upload(new ByteArrayInputStream(pdfByte), "案件审批表.pdf");
//            log.info("AdmreAuditRefuse 案件审批表生成并保存成功 -> {}", attachment.getId());
//        } catch (Exception e) {
//            log.error("AdmreAuditRefuse 案件审批表生成并保存失败 ->  msg[{}]", e.getMessage());
//            throw new RuntimeException("AdmreAuditAccept 案件受理审批表生成并保存失败 -> " + e.getMessage());
//        }
//
//        // 3. 保存审批系统文书, 只存父流程
//        Aprv_process_materials docObj = new Aprv_process_materials(Tp_procmaterialEnum.ADMRE_APPROVE);
//        docObj.setId(auditObj.getApplyid() + NanoIdUtils.randomNanoId());
//        // 通知书类型枚举
//        docObj.setMattype(Tp_procmaterial.ADMRE_REPORT);
//        docObj.setAttachid(attachment.getId());
//        docObj.setCreateuserid(this.userid);
//        docObj.setCreateuser(this.username);
//        docObj.setCreatetime(new Date());
//        docObj.setApplyid(auditObj.getApplyid());
//        docObj.setType(Tp_proctype.ACCEPT_ACCEPT);
//        docObj.setClassify(Tp_classify.DOCUMENT);
//        docObj.setSonapplyid(auditObj.getId());
//        this.aprv_process_materialsService.saveOrUpdate(docObj);
        return null;
    }
}
